// 创建一个SqliteHelper类，辅助sql语句的执行
#pragma once
#include <sqlite3.h>
#include <iostream>
#include <string>
class SqliteHelper{
    typedef int (*SqliteCallback)(void*,int,char**, char**);
public:
    SqliteHelper(const std::string& filename):_filename(filename){}
    bool open(int flag = SQLITE_OPEN_FULLMUTEX){
        int ret = sqlite3_open_v2(_filename.c_str(), &_handler, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | flag, nullptr);
        if(ret != SQLITE_OK){
            std::cout << "连接失败 ：" << sqlite3_errstr(ret) << std::endl;
            return false;
        }
        return true;
    }
    bool exec(const std::string& sql, SqliteCallback cb, void* args){
        int ret = sqlite3_exec(_handler, sql.c_str(), cb, args, nullptr);
        if(ret != SQLITE_OK){
            std::cout << sql << " 执行失败 reason: " << sqlite3_errmsg(_handler) << std::endl;
            return false;
        }
        return true;
    }
    void close(){
        if(_handler){
            sqlite3_close_v2(_handler);
        }
    }
private:
    std::string _filename;
    sqlite3* _handler;
};